package com.situ.shoplook.admin.api;

import com.situ.shoplook.common.model.Cart;
import com.situ.shoplook.common.model.CartTemp;
import com.situ.shoplook.common.service.CartService;
import com.situ.shoplook.common.util.PaginateInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author:yzy
 * @date:2024/9/5
 */
@RestController
@RequestMapping(value = "/api/cart", produces = "application/json;charset=utf-8")
public class CartApi {
    @DubboReference
    private CartService cartService;

    @GetMapping
    public Map<String, Object> findAll(
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        if (pageNo == null || pageNo < 1) {
            pageNo = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }

        System.out.println("pageSize = " + pageSize);
        PaginateInfo pi = new PaginateInfo(pageNo, pageSize);
        Map<String, Object> map = cartService.findAll(pi);
        List<Cart> carts = (List<Cart>) map.get("data");
        System.out.println("carts = " + carts);
        PaginateInfo p = (PaginateInfo) map.get("pi");
        Map<String, Object> map2 =Map.of("total", p.getTotal(), "pageNo", p.getPageNo(), "pageSize", p.getPageSize());

        return Map.of("success",true, "data", carts, "pi", map2);
    }
}
